package deepseek

import (
	"ai-bot/args"
	bytes2 "bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type ReqBody struct {
	Model    string     `json:"model"`
	Messages []Messages `json:"messages"`
	Stream   bool       `json:"stream"`
}
type Messages struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type RespBody struct {
	ID                string      `json:"id"`
	Choices           []Choices   `json:"choices"`
	Created           int         `json:"created"`
	Model             string      `json:"model"`
	SystemFingerprint interface{} `json:"system_fingerprint"`
	Object            string      `json:"object"`
	Usage             Usage       `json:"usage"`
}
type Message struct {
	Content string `json:"content"`
	Role    string `json:"role"`
}
type Choices struct {
	Index        int         `json:"index"`
	Message      Message     `json:"message"`
	FinishReason string      `json:"finish_reason"`
	Logprobs     interface{} `json:"logprobs"`
}
type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

/**




req

{
    "model": "deepseek-chat",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello!"
        }
    ],
    "stream": false
}


resp
{
    "id": "5db77377-4caf-4c13-a3ba-49b45936cb81",
    "choices": [
        {
            "index": 0,
            "message": {
                "content": " Hello! How can I assist you today? If you have any questions or need information on a particular topic, feel free to ask.",
                "role": "assistant"
            },
            "finish_reason": "stop",
            "logprobs": null
        }
    ],
    "created": 1715779533,
    "model": "deepseek-chat",
    "system_fingerprint": null,
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 17,
        "completion_tokens": 27,
        "total_tokens": 44
    }
}
*/

// sk-94dd0a933fa54ef8bf0979399707c66e

//var url string = "https://api.deepseek.com/chat/completions"
//var method string = "POST"
//var apiKey string = "sk-94dd0a933fa54ef8bf0979399707c66e"
//var model string = "deepseek-chat"

func SendToDeepSeek(msg string) string {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(fmt.Sprintf("发生了错误。。。 %v", err))
		}
	}()
	fmt.Println("send to deepseek start......")
	reqBody := ReqBody{
		Model: args.DeepSeek.Model,
		Messages: []Messages{
			Messages{
				Role:    "user",
				Content: msg,
			},
		},
		Stream: false,
	}
	bytes, e := json.Marshal(&reqBody)
	if e != nil {
		fmt.Println("err..... send deepseek...")
		return ""
	}
	client := &http.Client{}
	req, err := http.NewRequest(args.DeepSeek.Method, args.DeepSeek.Url, bytes2.NewBuffer(bytes))

	if err != nil {
		fmt.Println(err)
		return ""
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer "+args.DeepSeek.ApiKey)

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	respBody := RespBody{}
	json.Unmarshal(body, &respBody)
	//fmt.Println(string(body))
	fmt.Println(respBody)

	fmt.Println("send to deepseek end......")
	return respBody.Choices[0].Message.Content
}
